//
//  PicContainer.m
//  ExtModule
//
//  Created by CPX on 2018/9/12.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "PicContainer.h"
#import <HMSegmentedControl.h>
#import "PicListController.h"
/**
 贺卡图片、gif 选择容器类
 author:王连友
 */
@interface PicContainer ()
@property (nonatomic,strong)HMSegmentedControl * seg;

@property (nonatomic,strong)UIViewController * currentViewController;
@property (nonatomic,strong)PicListController * picVC;
@property (nonatomic,strong)PicListController * gifVC;


@end

@implementation PicContainer

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initViews{
    _picVC = [[PicListController alloc]init];
    _picVC.type = 0;
    
    _gifVC = [[PicListController alloc]init];
    _gifVC.type = 1;
    self.currentViewController = _picVC;
    [self addChildViewController:self.picVC];
    [self addChildViewController:self.gifVC];
    [self.view addSubview:self.picVC.view];
    self.picVC.view.frame = CGRectMake(0, NavHeight, self.view.width, self.view.height - NavHeight);
    self.gifVC.view.frame = CGRectMake(0, NavHeight, self.view.width, self.view.height - NavHeight);

    
}

- (void)initNavigation{
    [self initDefaultNavigation];
    NSArray* titles;
    if (@available(iOS 9.0, *)) {
        if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
            titles = @[TLString(@"greeting_cards_Gifs"),TLString(@"greeting_cards_Images")];
        }
        else{
            titles = @[TLString(@"greeting_cards_Images"),TLString(@"greeting_cards_Gifs")];
        }
    }
    else {
        // Fallback on earlier versions
        titles = @[TLString(@"greeting_cards_Images"),TLString(@"greeting_cards_Gifs")];

    }
    self.seg = [[HMSegmentedControl alloc]initWithSectionTitles:titles];
    self.seg.backgroundColor = [UIColor clearColor];
    self.seg.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.seg.titleTextAttributes = @{NSForegroundColorAttributeName :[UIColor blackColor] ,NSFontAttributeName:[UIFont systemFontOfSize:16]};
    self.seg.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : UIColorHex(#00732F)};
    self.seg.selectionIndicatorHeight = 2.0f;
    self.seg.selectionIndicatorColor = UIColorHex(#00732F);
    [self.myNavigationBar addSubview:self.seg];
    [self.seg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.myNavigationBar);
        make.centerY.equalTo(self.myNavigationBar.backButton);
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(40);
    }];
    if (@available(iOS 9.0, *)) {
        if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
            self.seg.selectedSegmentIndex = 1;
        }
        else{
            self.seg.selectedSegmentIndex = 0;
        }
    }
    else {
        self.seg.selectedSegmentIndex = 0;
    }

    
    WeakSelf;
    [self.seg setIndexChangeBlock:^(NSInteger index) {
        if (@available(iOS 9.0, *)) {
            if ([UIView appearance].semanticContentAttribute == UISemanticContentAttributeForceRightToLeft) {
                if (index == 0){
                    [weakSelf replaceController:weakSelf.currentViewController newController:weakSelf.gifVC];
                    [weakSelf logEvent:@"gc_click_Gifs"];

                }
                else{
                    [weakSelf replaceController:weakSelf.currentViewController newController:weakSelf.picVC];
                    [weakSelf logEvent:@"gc_click_images"];
                }
            }
            else{
                if (index == 0){
                    [weakSelf replaceController:weakSelf.currentViewController newController:weakSelf.picVC];
                    [weakSelf logEvent:@"gc_click_images"];

                }
                else{
                    [weakSelf replaceController:weakSelf.currentViewController newController:weakSelf.gifVC];
                    [weakSelf logEvent:@"gc_click_Gifs"];

                }
            }
        }
        else {
            // Fallback on earlier versions
            if (index == 0){
                [weakSelf replaceController:weakSelf.currentViewController newController:weakSelf.picVC];
            }
            else{
                [weakSelf replaceController:weakSelf.currentViewController newController:weakSelf.gifVC];
            }
        }
        
      
    }];
    
}

- (void)replaceController:(UIViewController *)oldController newController:(UIViewController *)newController{
    if (newController  == oldController){
        return;
    }
    [self transitionFromViewController:oldController toViewController:newController duration:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
    }  completion:^(BOOL finished) {
       
        
        
        self.currentViewController = newController;
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
